package com.tiza.xiaoyoubao.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.tianze.library.adapter.recycler.RecyclerBaseAdapter;
import com.tianze.library.base.BaseStoreHouseRefreshRecyclerFragment;
import com.tianze.library.view.EmptyLayout;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.app.Constants;
import com.tiza.xiaoyoubao.app.UIHelper;
import com.tiza.xiaoyoubao.entity.DianFuTbTaskOrderInfo;
import com.tiza.xiaoyoubao.entity.UserInfo;
import com.tiza.xiaoyoubao.net.ApiHttpClient;
import com.tiza.xiaoyoubao.net.JsonCallback;
import com.tiza.xiaoyoubao.net.ResponseInfo;
import com.tiza.xiaoyoubao.utils.DateUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Request;

/**
 * Created by mapple on 17/4/10.
 * 垫付特别任务
 */

public class DianFuTbTaskFragment extends BaseStoreHouseRefreshRecyclerFragment {

    UserInfo userInfo = Hawk.get(Constants.KEY_USER_INFO, (UserInfo) null);
    private List<DianFuTbTaskOrderInfo> infoList;
    private List<DianFuTbTaskOrderInfo> mList = new ArrayList<>();

    private static int Year, Month, Day, Hour, Minute;
    private static SimpleDateFormat df;
    //时间差
    private static long minute;

    @Override
    protected RecyclerBaseAdapter getListAdapter() {
        return new DianFuTbTaskListAdapter();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        selectOver();
        pageSize = Integer.MAX_VALUE;
        df = new SimpleDateFormat("yyyy-MM-dd HH:mm");//设置日期格
    }

    @Override
    protected void onItemClick(View view, int position) {
        super.onItemClick(view, position);
        DianFuTbTaskOrderInfo info = (DianFuTbTaskOrderInfo) mAdapter.getData().get(position);
        Hawk.put(Constants.KEY_DIANFUTBTASKOEDER_INFO, info);
        Bundle args = new Bundle();
        args.putSerializable("info", info);
        UIHelper.showDianFuTbTaskDetial(mActivity, args);
        finish();
    }

    public static class DianFuTbTaskListAdapter extends RecyclerBaseAdapter {

        @Override
        protected View onCreateItemView(ViewGroup parent, int viewType) {
            return getLayoutInflater(parent.getContext()).inflate(R.layout.item_dianfu_tb_task,
                    parent, false
            );
        }

        @Override
        protected ViewHolder onCreateItemViewHolder(View view, int viewType) {
            return new ViewHolder(viewType, view);
        }

        @Override
        public void onBindItemViewHolder(RecyclerBaseAdapter.ViewHolder holder, int position) {
            super.onBindItemViewHolder(holder, position);
            final ViewHolder vh = (ViewHolder) holder;
            DianFuTbTaskOrderInfo info = (DianFuTbTaskOrderInfo) _data.get(position);
            vh.tv_money.setText(DateUtils.formateRate(info.getBaseCmi()));
            vh.bt_type.setText(info.getBackName());
            vh.tv_taskNum.setText("剩余" + info.getTaskNumber() + "单");
            vh.tv_taskId.setText("ID:" + info.getUserid());
            vh.tv_taskMoney.setText(info.getPriceSection() + "元");
            vh.tv_goodType.setText(info.getEntranceName());
        }

        static class ViewHolder extends RecyclerBaseAdapter.ViewHolder {
            @BindView(R.id.tv_taskId)
            TextView tv_taskId;
            @BindView(R.id.tv_taskMoney)
            TextView tv_taskMoney;
            @BindView(R.id.tv_money)
            TextView tv_money;
            @BindView(R.id.bt_type)
            Button bt_type;
            @BindView(R.id.tv_taskNum)
            TextView tv_taskNum;
            @BindView(R.id.tv_goodType)
            TextView tv_goodType;

            ViewHolder(int viewType, View view) {
                super(viewType, view);
                ButterKnife.bind(this, view);
            }
        }
    }
    
    @Override
    protected void requestData(boolean refresh) {
        super.requestData(refresh);
        ApiHttpClient.getSellerTaskEcpinfoAll(this, new JsonCallback<DianFuTbTaskOrderInfo>(DianFuTbTaskOrderInfo.class) {
            @Override
            public void onError(Call call, Exception e) {
                onRequestError();
            }

            @Override
            public void onResponse(ResponseInfo<DianFuTbTaskOrderInfo> response) {
                int code = response.getCode();
                if (code == 1) {
                    mErrorLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
                    return;
                }
                infoList = response.getData();
                onRequestResponse(infoList);
            }

            @Override
            public void onAfter() {
                super.onAfter();
                onRequestFinish();
            }
        });
    }
    private void selectOver() {
        ApiHttpClient.selectOver(this,
                                 new JsonCallback<String>(String.class,true) {

                                     @Override
                                     public void onBefore(Request request) {
                                         super.onBefore(request);
                                     }

                                     @Override
                                     public void onError(Call call, Exception e) {
                                         toast("网络连接失败，请稍后重试");
                                     }

                                     @Override
                                     public void onResponse(ResponseInfo<String> response) {
                                         int code = response.getCode();
                                         String message = response.getMessage();
                                         if (code == 1) {
                                             showWarnDialog(message);
                                             return;
                                         }
                                         Map<String, Object> _map = response.getFirstEntity();
                                     }

                                     @Override
                                     public void onAfter() {
                                         super.onAfter();
                                     }
                                 }
        );
    }
}
